package com.example.seckill.controller;

import com.example.entity.DateUtil;
import com.example.entity.Result;
import com.example.entity.StatusCode;
import com.example.seckill.pojo.SeckillGoods;
import com.example.seckill.service.SeckillGoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/seckillGoods")
@CrossOrigin
public class SeckillGoodsController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    /***
     * SeckillGoods分页条件搜索实现
     * @param seckillGoods SeckillGoods
     * @param page 当前页
     * @param size 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<SeckillGoods>> findPage(@RequestBody(required = false) SeckillGoods seckillGoods, @PathVariable int page, @PathVariable int size) {
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(seckillGoods, page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * SeckillGoods分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return Result<PageInfo<SeckillGoods>>
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<SeckillGoods>> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param seckillGoods SeckillGoods
     * @return Result<List<SeckillGoods>>
     */
    @PostMapping(value = "/search")
    public Result<List<SeckillGoods>> findList(@RequestBody(required = false) SeckillGoods seckillGoods) {
        List<SeckillGoods> list = seckillGoodsService.findList(seckillGoods);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id ID
     * @return Result<SeckillGoods>
     */
    @DeleteMapping(value = "/{id}")
    public Result<SeckillGoods> delete(@PathVariable Long id) {
        seckillGoodsService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改SeckillGoods数据
     * @param seckillGoods SeckillGoods
     * @param id ID
     * @return Result<SeckillGoods>
     */
    @PutMapping(value = "/{id}")
    public Result<SeckillGoods> update(@RequestBody SeckillGoods seckillGoods, @PathVariable Long id) {
        seckillGoods.setId(id);
        seckillGoodsService.update(seckillGoods);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增SeckillGoods数据
     * @param seckillGoods SeckillGoods
     * @return Result<SeckillGoods>
     */
    @PostMapping
    public Result<SeckillGoods> add(@RequestBody SeckillGoods seckillGoods) {
        seckillGoodsService.add(seckillGoods);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询SeckillGoods数据
     * @param id ID
     * @return Result<SeckillGoods>
     */
    @GetMapping("/{id}")
    public Result<SeckillGoods> findById(@PathVariable Long id) {
        SeckillGoods seckillGoods = seckillGoodsService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", seckillGoods);
    }

    /***
     * 查询SeckillGoods全部数据
     * @return Result<List<SeckillGoods>>
     */
    @GetMapping
    public Result<List<SeckillGoods>> findAll() {
        List<SeckillGoods> list = seckillGoodsService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 获取当前的时间基准的5个时间段
     * @return List<Date>
     */
    @GetMapping("/menus")
    public List<Date> datemenus() {
        List<Date> dateMenus = DateUtil.getDateMenus();
        for (Date date : dateMenus) {
            System.out.println(date);
        }
        return dateMenus;
    }

    /**
     * 根据时间段(2019090516) 查询该时间段的所有的秒杀的商品
     * @param time 时间
     * @return List<SeckillGoods>
     */
    @RequestMapping("/list")
    public List<SeckillGoods> list(String time){
        List<SeckillGoods> seckillGoodsList = seckillGoodsService.list(time);
        for (SeckillGoods seckillGoods : seckillGoodsList) {
            System.out.println("id是:"+seckillGoods.getId());
        }
        return seckillGoodsList;
    }

    /**
     * 根据时间段  和秒杀商品的ID 获取商的数据
     * @param time 时间
     * @param id ID
     * @return SeckillGoods
     */
    @GetMapping("/one")
    public SeckillGoods one(String time,Long id){

        return  seckillGoodsService.one(time,id);
    }
}
